package com.yanqu.road.server.cmd.activity.springdinner;

import com.yanqu.road.entity.activity.springdinner.BallData;
import com.yanqu.road.entity.activity.springdinner.ComposeData;
import com.yanqu.road.pb.activity.SpringDinnerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.springdinner.SpringDinnerModule;
import com.yanqu.road.server.pb.SpringDinnerPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Cmd(code = Protocol.S_SPRINGDINNER_GAME_CLEAR, desc = "请求进入合成大西瓜游戏界面")
public class SpringDinnerGameClearCmd extends AbstractCommand {

    private int activityId;
    private int type;
    private int ballId;
    private Map<Integer, BallData> ballMap;
    private Map<Integer, ComposeData> composeMap;

    SpringDinnerProto.SpringDinnerClearRespMsg.Builder respMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        SpringDinnerProto.SpringDinnerClearReqMsg reqMsg = SpringDinnerProto.SpringDinnerClearReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        type = reqMsg.getType();
        ballId = reqMsg.getFruitId();
        ballMap = new ConcurrentHashMap<>();
        ret = SpringDinnerPb.parseReqBallData(ballMap,reqMsg.getFruitsList());
        if(ret != 0)return true;
        composeMap = new ConcurrentHashMap<>();
        ret = SpringDinnerPb.parseReqComposeData(composeMap,reqMsg.getComposeDataList());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(ret != 0){
            return;
        }
        ret = player.getModule(SpringDinnerModule.class).checkCommon(activityId,type);
        if(ret != 0){
            return;
        }

        respMsg = player.getModule(SpringDinnerModule.class).clearBall(type,ballId,ballMap,composeMap);

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            respMsg = SpringDinnerProto.SpringDinnerClearRespMsg.newBuilder();
            respMsg.setRet(ret);
        }
        player.sendPacket(Protocol.U_SPRINGDINNER_GAME_CLEAR, respMsg);
    }

}
